﻿using SimulationPTSystem.EnvironmentDevice.Input;
using SimulationPTSystem.EnvironmentDevice.Models;
using SimulationPTSystem.EnvironmentDevice.ViewModels;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace SimulationPTSystem.EnvironmentDevice.Test
{
    /// <summary>
    /// VideoWindow.xaml 的交互逻辑
    /// </summary>
    public partial class VideoWindow : Window
    {
        public DeviceViewModel DeviceViewModel { get; set; }

        public static readonly DependencyProperty DeviceModelsProperty =
            DependencyProperty.Register(nameof(DeviceModels), typeof(List<DeviceModel>), typeof(VideoWindow));
        public List<DeviceModel> DeviceModels { get => (List<DeviceModel>)GetValue(DeviceModelsProperty); set => SetValue(DeviceModelsProperty, value); }


        public VideoWindow()
        {
            InitializeComponent();

            Media.MediaOpened += Media_MediaOpened;

            DeviceViewModel = new DeviceViewModel();
            DeviceViewModel.Initialize();
            DeviceViewModel.ModelsChanged += DeviceViewModel_ModelsChanged;
            DeviceViewModel.Started += DeviceViewModel_Started;
            DeviceViewModel.Stoped += DeviceViewModel_Stoped;

            DataContext = this;

            Hide();

            //string videoSource = ConfigurationManager.AppSettings["VideoSource"];
            //if (File.Exists(videoSource))
            //{
            //    Media.Source = new Uri(videoSource);
            //}
        }

        private void DeviceViewModel_Stoped(object sender, EventArgs e)
        {
            //Dispatcher.BeginInvoke(new Action(() => Media.Stop()));
        }

        private void DeviceViewModel_Started(object sender, EventArgs e)
        {
            Stopwatch.Restart();
            DeviceViewModel.DataConverter.DataReceived -= DataInput_DataReceived;
            DeviceViewModel.DataConverter.DataReceived += DataInput_DataReceived;
            //Dispatcher.BeginInvoke(new Action(() =>
            //{
            //    Media.Position = TimeSpan.Zero;
            //    Media.Play();
            //}));
        }

        private void Media_MediaOpened(object sender, RoutedEventArgs e)
        {
            DeviceViewModel.Start();
        }

        System.Diagnostics.Stopwatch Stopwatch = new System.Diagnostics.Stopwatch();

        private void DataInput_DataReceived(object sender, DataReceivedEventArgs e)
        {
            if (e.Data != null && e.Data.Length == 3)
                Dispatcher.BeginInvoke(new Action(() =>
                {
                    TextBlock.Text = $"测试时间：{Stopwatch.Elapsed} 时间：{e.Time}  设备类型：{e.Data[0]}  设备编号：{e.Data[1]}  设备状态：{e.Data[2]}";
                }));
        }

        private void DeviceViewModel_ModelsChanged(object sender, EventArgs e)
        {
            Dispatcher.BeginInvoke(new Action(() => DeviceModels = new List<DeviceModel>(DeviceViewModel.Models)));
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            DeviceViewModel.Start();
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            base.OnClosing(e);

            DeviceViewModel.Stop();
        }
    }
}
